/*
 * @ : Copyright (c) 2021 Phytium Information Technology, Inc. 
 *  
 * SPDX-License-Identifier: Apache-2.0.
 * 
 * @Date: 2021-09-16 10:30:25
 * @LastEditTime: 2021-09-18 13:31:16
 * @Description:  This files is for 
 * 
 * @Modify History: 
 *  Ver   Who        Date         Changes
 * ----- ------     --------    --------------------------------------
 */
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "strto.h"
#include "generic_timer.h"
#include "sdkconfig.h"
#ifndef SDK_CONFIG_H__
    #warning "Please include sdkconfig.h"
#endif
#include "ft_debug.h"
#include "parameters.h"
#include "../src/shell.h"
#include "sdio_app.h"

static sdmmc_host_t sdio_host;
static sdmmc_card_t sdio_card;
static FSdioCtrl    sdio_ctrl;
static boolean init_done = FALSE;

static int MmcInit()
{
    memset(&sdio_host, 0, sizeof(sdio_host));
    memset(&sdio_card, 0, sizeof(sdio_card));
    memset(&sdio_ctrl, 0, sizeof(sdio_ctrl));

    sdio_host_config(FSDIO_HOST_INSTANCE_0, &sdio_host, &sdio_card, &sdio_ctrl);
    if (SDMMC_OK != sdio_host_init())
    {
        printf("sdio host init failed\r\n");
        return -1;
    }

    init_done = TRUE;
    return 0;
}

static int MmcSetString(const char *string, u32 blk)
{
    static char write_buf[FSDIO_DEF_BLK_SIZE];

    memset(write_buf, 0, FSDIO_DEF_BLK_SIZE);
    memcpy(write_buf, string, strlen(string) + 1);
    return sdio_host_write_sectors(&sdio_host, blk, 1, write_buf);
}

static int MmcGetString(u32 blk)
{
    static char read_buf[FSDIO_DEF_BLK_SIZE];
    int ret;

    memset(read_buf, 0 , FSDIO_DEF_BLK_SIZE);
    ret = sdio_host_read_sectors(&sdio_host, blk, 1, read_buf);
    FtDumpHexByte(read_buf, FSDIO_DEF_BLK_SIZE);
    return ret;
}

static void MmcUsage()
{
	printf("usage:\r\n");
	printf("    mmc probe\r\n");
    printf("        -- print mmcsd card info\r\n"); 
    printf("    mmc set [target_blk] [string]\r\n");
    printf("        -- set a string to target mmc block\r\n");
    printf("    mmc get [source_blk]\r\n");
    printf("        -- get a string from source mmc block\r\n");    
}

static int MmcCmdEntry(int argc, char *argv[])
{
	u32 blk, cnt, align;
	void *addr;
    char *str;
    int ret = 0;

    if(argc < 2)
	{
		MmcUsage();
        return -1;
	}

    if (!init_done)
    {
        if (0 != MmcInit())
        {
            return -1;
        }
    }

    if(!strcmp(argv[1], "probe"))
    {
        ret = MmcInit();
    }
    else if (!strcmp(argv[1], "set"))
    {
        if(argc < 4)
        {
            MmcUsage();
            return -1;
        }

        blk = simple_strtoul(argv[2], NULL, 16);
        str = argv[3];
        ret = MmcSetString(str, blk);
    }
    else if (!strcmp(argv[1], "get"))
    {
        if(argc < 3)
        {
            MmcUsage();
            return -1;
        }

        blk = simple_strtoul(argv[2], NULL, 16);
        ret = MmcGetString(blk);
    }
    
    return ret;
}
SHELL_EXPORT_CMD(SHELL_CMD_TYPE(SHELL_TYPE_CMD_MAIN), mmc, MmcCmdEntry, test sdio driver);